﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
namespace TruongVuWebsite.UserControls
{
    public partial class ucExchangeRate : System.Web.UI.UserControl
    {
        private const string url = "http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetRateExchange();
            }
        }

        private void GetRateExchange()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(url);
            var htmlBuilder = new StringBuilder();
            htmlBuilder.Append("<table class='data' >");
            htmlBuilder.Append("<tr><th>Mã NT</th><th>Mua TM</th><th>Mua CK</th><th>Bán</th></tr>");
            
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[1].Rows) 
                {
                    htmlBuilder.AppendFormat("<tr><td><b>{0}</b></td><td>{1}</td><td>{2}</td><td>{3}</td></tr>",
                        row["CurrencyCode"].ToString(),
                        (int)Math.Round(double.Parse(row["Buy"].ToString(), CultureInfo.InvariantCulture.NumberFormat)),
                        (int)Math.Round(double.Parse(row["Transfer"].ToString(), CultureInfo.InvariantCulture.NumberFormat)),
                        (int)Math.Round(double.Parse(row["Sell"].ToString(), CultureInfo.InvariantCulture.NumberFormat)));
                }
            }
            htmlBuilder.Append("</table>");

            this.litExRate.Text = htmlBuilder.ToString();
        }
    }
}